﻿using LPP.Entity.AppUser;
using System;
using LPP.Entity.Work;
using System.ComponentModel.DataAnnotations.Schema;

namespace LPP.Entity.Orders
{
    public class Order
    {
        public string OrderID { get; set; }
        public int UserID { get; set; }
        public int WorkerID { get; set; }
        public decimal Amount { get; set; }
        public OrderStatus Status { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime? CompletedTime { get; set; }
        public DateTime? PaidAt { get; set; }
        public PaymentMethod PaymentMethod { get; set; }
        /// <summary>
        /// 支付状态
        /// </summary>
        public PaymentStatus PaymentStatus { get; set; }

        public string Description { get; set; }
        public DateTime? AppointmentTime { get; set; }
        /// 交易号（支付平台返回）
        /// </summary>
        public string TransactionId { get; set; }
        /// <summary>
        /// 支付相关信息（JSON格式）
        /// </summary>
        public string PaymentInfo { get; set; }


        // 导航属性
        public virtual Users User { get; set; }
        public virtual ICollection<OrderDetail> OrderDetails { get; set; }

        [NotMapped]
        public decimal WorkerIncome
        {
            get => OrderDetails?.Sum(od => od.WorkerIncome) ?? 0m;
        }

        [NotMapped]
        public decimal PlatformIncome
        {
            get => OrderDetails?.Sum(od => od.UnitPrice - od.WorkerIncome) ?? 0m;
        }

        public Order()
        {
            OrderDetails = new List<OrderDetail>();
            OrderID = Guid.NewGuid().ToString();
            Status = OrderStatus.Pending;
            CreatedAt = DateTime.Now;
        }
    }
    // 订单状态枚举
    public class OrderDetail
    {
        public string OrderDetailID { get; set; } // 订单明细ID
        public string OrderID { get; set; } // 关联订单的ID（外键） 
        public int? WorkerID { get; set; }  // 关联的工人ID
        public DateTime AssignedAt { get; set; } // 接单时间
        public DateTime? CompletedAt { get; set; } // 完成时间
        public string ProductName { get; set; } // 商品名称
        public DateTime? AppointmentTime { get; set; }
        public OrderStatus Status { get; set; } = OrderStatus.Pending;  // 状态字段
        public decimal UnitPrice { get; set; }
        public decimal? Rating { get; set; }  // 添加评分属性
        public int Quantity { get; set; }

        // 存储属性
        private decimal _totalPrice;
        public decimal TotalPrice
        {
            get => _totalPrice;
            set => _totalPrice = value;
        }
        private decimal _workerIncome;
        private decimal _platformIncome;
        private decimal _commissionRate = 0.5m; // 默认佣金率为50%
        [NotMapped]
        public decimal WorkerIncome
        {
            get => _workerIncome;
            set => _workerIncome = value;
        }

        [NotMapped]
        public decimal PlatformIncome
        {
            get => _platformIncome;
            set => _platformIncome = value;
        }

        [NotMapped]
        public decimal CommissionRate
        {
            get => _commissionRate;
            set => _commissionRate = value > 1 ? 1 : (value < 0 ? 0 : value);
        }
        // 计算收入的方法
        public void CalculateIncome()
        {
            if (Order == null || TotalPrice == null)
            {
                throw new InvalidOperationException("订单信息不完整，无法计算收入。");
            }
            PlatformIncome = TotalPrice * CommissionRate;
            WorkerIncome = TotalPrice - PlatformIncome;
        }

        // 设置佣金率并重新计算收入的方法
        public void SetCommissionRateAndCalculate(decimal rate)
        {
            CommissionRate = rate;
            CalculateIncome();
        }
        // 导航属性，指向订单实体
        public virtual Order Order { get; set; }
        public virtual Worker Worker { get; set; }

        // 构造函数
        public OrderDetail()
        {
            OrderDetailID = Guid.NewGuid().ToString(); // 自动生成唯一订单明细ID
            UpdateTotalPrice(); // 初始化时计算总价
        }

        public void UpdateTotalPrice()
        {
            _totalPrice = UnitPrice * Quantity;
        }
    }
}

    public enum OrderStatus
    {
        /// <summary>
        /// 待接单
        /// </summary>
        Pending,

        /// <summary>
        /// 已接单，进行中
        /// </summary>
        InProgress,

        /// <summary>
        /// 已完成
        /// </summary>
        Completed,

        /// <summary>
        /// 已取消
        /// </summary>
        Cancelled,

        /// <summary>
        /// 已评价
        /// </summary>
        Reviewed
    }
   /* public enum OrderStatus
    {
        Pending,   // 待支付
        Paid,      // 已支付
        Completed, // 已完成
        Cancelled  // 已取消
    }*/

    public enum PaymentMethod
    {
        Alipay,    // 支付宝
        WeChatPay, // 微信支付
        CreditCard // 信用卡
        // 其他支付方式可以在此处扩展
    }




